package org.flower.web.servlet;

import com.github.pagehelper.PageInfo;
import org.flower.bean.User;
import org.flower.service.UserService;
import org.flower.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Aizen
 */
@WebServlet("/manager/userListServlet")
public class UserListServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取页码、如果页码为空默认=1
        String pageNo = request.getParameter("pageNo");
        if(pageNo==null||pageNo.equals("")){
            pageNo = "1";
        }

        //封装模糊查询条件
        User user = new User();
        Object date = request.getSession().getAttribute("date");
        if(date!=null&&!date.equals("")){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date parse = sdf.parse((String)date);
                user.setLogindate(parse);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Object name = request.getSession().getAttribute("name");
        if(name!=null&&!name.equals("")){
            user.setName((String)name);
        }
        Object sex = request.getSession().getAttribute("sex");
        if(sex!=null&&!sex.equals("")){
            user.setSex((String) sex);
        }
        Object phone = request.getSession().getAttribute("phone");
        if(phone!=null&&!phone.equals("")){
            user.setPhone((String) phone);
        }
        Object state = request.getSession().getAttribute("state");
        if(state!=null&&!state.equals("")){
            user.setState(Boolean.parseBoolean((String)state));
            request.getSession().setAttribute("state",state);
        }

        //判断选择的什么状态
        if(state!=null&&!state.equals("")){
            user.setFlag(true);
            if("true".equals(state)){
                user.setState(true);
            }else{
                user.setState(false);
            }
        }


        PageInfo<User> userPageInfo = null;
        try {
            userPageInfo = userService.findAll(user, Integer.parseInt(pageNo));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        request.getSession().setAttribute("userPageInfo", userPageInfo);
        response.sendRedirect("userList.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String pageNo = request.getParameter("pageNo");
        if(pageNo==null||pageNo.equals("")){
            pageNo = "1";
        }

        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String sex = request.getParameter("sex");
        String date = request.getParameter("date");
        String state = request.getParameter("state");

        request.getSession().setAttribute("name",name);
        request.getSession().setAttribute("phone",phone);
        request.getSession().setAttribute("sex",sex);
        request.getSession().setAttribute("date",date);
        request.getSession().setAttribute("state",state);

        User user = new User();
        user.setName(name);
        user.setState(Boolean.parseBoolean(state));
        user.setSex(sex);
        user.setPhone(phone);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = null;
            if(date!=null&&!date.equals("")){
                parse = sdf.parse(date);
            }
            user.setLogindate(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //判断选择的什么状态
        if(state!=null&&!state.equals("")){
            user.setFlag(true);
            if("true".equals(state)){
                user.setState(true);
            }else{
                user.setState(false);
            }
        }

        PageInfo<User> userPageInfo = null;
        try {
            userPageInfo = userService.findAll(user, Integer.parseInt(pageNo));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        request.getSession().setAttribute("userPageInfo", userPageInfo);
        response.sendRedirect("userList.jsp");
    }
}
